var app = getApp()
import urls from '../../utils/urls.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        schoolId: 0,
        alldetail: '', //所有的详情
        allClass: [], //所有课程
        status: 0, //navtab第几个
        haslike: false, //已关注
        list: [],
        comments: [], //评论
        imgUrls: [],
        textelllipsis: true, //显示全部
        sharetip: false, //分享弹框
        nearschool: [], //附近机构
        teachers: [],
        visitorId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('机构详情参数：', options);
        var that = this;
        if (options.scene) {
            // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
            const scene = decodeURIComponent(options.scene).split('&')[0].split('=')[1];
            options.schoolId = scene;
        };
        this.setData({
            schoolId: options.schoolId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

 

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        
        wx.showLoading();
        app.getLocation(() => {
            that.get_school_info();
            that.get_near_shcool();
            
        })

        that.get_school_teacher();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        if(this.data.visitorId){
            app.visitorLeave(this.data.visitorId, () => {
                console.log('发送访客离开时间成功');
            });
        }
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        if(this.data.visitorId){
            app.visitorLeave(this.data.visitorId, () => {
                console.log('发送访客离开时间成功');
            });
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        this.setData({
            sharetip: false
        })

        return {
            title: '都说这个学校好，你要不跟我一起去看看？'
        }
    },

    //机构信息
    get_school_info() {
        let that = this;
        app.request({
            url: 'school/detail',
            data: {
                schoolId: that.data.schoolId
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    wx.hideLoading();
                    that.setData({
                        alldetail: res.data,
                        imgUrls: res.data.schoolImages,
                        haslike: res.data.focus ? true : false,
                    })

                    that.get_comments(res.data.schoolId);
                    app.visitorEnter({
                        title: '机构-'+res.data.schoolName,
                        schoolId: that.data.schoolId,
                        courseId: '',
                        teacherId: '',
                        from: 1
                    }, (visitorId) => {
                        that.setData({
                            visitorId: visitorId
                        });
                    });
                } else {
                    app.alert(res.msg,() => {
                        wx.navigateBack();
                    })
                }
                
            }
        })

        
    },
    //机构老师
    get_school_teacher() {
        let that = this;
        app.request({
            url: 'school/teacher',
            data:{
                school_id: that.data.schoolId
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    that.setData({
                        teachers: res.data
                    })
                }
            }
        })
    },
    //附近机构
    get_near_shcool() {
        var that = this;
        app.request({
            url: 'school/nearby',
            data: {
                school_id: that.data.schoolId
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    that.setData({
                        nearschool: res.data
                    })
                }
            }
        })
    },
    //评论列表
    get_comments: function(schoolId) { 
        var that = this;
        app.request({
            url: 'remark/listSchool',
            data: {
                school_id: that.data.schoolId
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        comments: res.data
                    })
                }
            }
        })
    },
    //关注
    like: function() { 
        var that = this;
        app.request({
            url: 'school/focus',
            data: {
                school_id: that.data.schoolId
            },
            sCallback: (res) => {
                var likes = !that.data.haslike
                that.setData({
                    haslike: likes
                })
                wx.showToast({
                    title: '操作成功',
                })
            }
        })

    },
    //想学
    want: function() { 
        var that = this;
        app.request({
            url: 'school/want',
            data: {
                school_id: that.data.schoolId
            },
            sCallback: (res) => {
                wx.showToast({
                    title: '操作成功',
                })
            }
        })
    },
    //回复评论
    reply: function(e) { 
        wx.navigateTo({
            url: '/pages/replay/index?parentRemarkId=' + e.currentTarget.dataset.id + '&schoolId=' + this.data.alldetail.schoolId,
        })
    },
    //评论点赞
    like_remark: function(e) { 
        var that = this;
        app.request({
            url: 'remark/likeSchool',
            data: {
                remarkId: e.currentTarget.dataset.id
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    that.get_comments();
                    wx.showToast({
                        title: '点赞成功',
                    })
                } else {
                    app.alert(res.msg);
                }
            }
        })
    },
    //已学
    teacherme() { 
        var that = this;
        app.request({
            url: 'school/learned',
            data: {
                school_id: that.data.schoolId
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    wx.navigateTo({
                        url: '/pages/score/index',
                    })
                } else {
                    app.alert(res.msg);
                }
            }
        })
        

    },
    //显示所有内容
    allellipsisFn: function() { 
        this.setData({
            textelllipsis: false
        })
    },
    //隐藏所有内容
    ellipsisFn: function() { 
        this.setData({
            textelllipsis: true
        })
    },
    //分享弹框
    show_share: function() { 
        this.setData({
            sharetip: true
        })
    },
    //分享到朋友圈
    share_circle: function() { 
        wx.navigateTo({
            url: '/pages/share/index?type=school',
        })
        this.setData({
            sharetip: false
        })
    },
    //预览图片
    preview_image: function(e) { 
        wx.previewImage({
            current: e.currentTarget.dataset.url, // 当前显示图片的http链接
            urls: this.data.imgUrls // 需要预览的图片http链接列表
        })
    },
    //隐藏分享选项
    hideMask() {
        this.setData({
            sharetip: false
        })
    },
    //打开地图
    openMap() {
        let data = this.data.alldetail;
        let latitude = parseFloat(data.lat);
        let longitude = parseFloat(data.lng);
        wx.openLocation({
            latitude: latitude, // 必需
            longitude: longitude, // 必需
            name: data.schoolName,
            address: data.addr,
            success: (res) => {

            },
            fail: (res) => {

            },
            complete: (res) => {

            }
        })
    },
    show_image(e) {
        let url = e.currentTarget.dataset.url;
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
        })
    },
})